package GUI.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;

import GUI.models.TreeNodeImplProperties;
import GUI.view.Calendar.MyCalen;

import client.domainModels.NodeImpl.AbstractNode;

public class TreeConfModifyPanelFormule extends JPanel implements
		ActionListener, FocusListener {

	JPanel fields;

	JPanel tablesPanel;

	static int eventID = 0;

	boolean addressSet = false;
	final static int GAP = 10;
	XMLFileTree tree;
	JPanel leftHalf;
	AbstractNode currentNode;
	public TreeConfModifyPanelFormule(XMLFileTree tree, JPanel tablesPanel) {
		this.tree = tree;
		this.tablesPanel = tablesPanel;
		currentNode = tree.getLastNode().getPropretiesObj();
		
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

		leftHalf = new JPanel() {
			// Don't allow us to stretch vertically.
			public Dimension getMaximumSize() {
				Dimension pref = getPreferredSize();
				return new Dimension(Integer.MAX_VALUE, pref.height);
			}
		};
		leftHalf.setLayout(new BoxLayout(leftHalf, BoxLayout.PAGE_AXIS));
		leftHalf.add(createEntryFields());
		leftHalf.add(createButtons());

		add(leftHalf);

	}

	protected JComponent createButtons() {
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.TRAILING));

		JButton button = new JButton("Set");
		button.addActionListener(this);
		panel.add(button);
		panel.setBorder(BorderFactory.createEmptyBorder(0, 0, GAP - 5, GAP - 5));
		return panel;
	}

	protected JComponent createLabels(String id) {
		JPanel panel = new JPanel(new BorderLayout());
		JLabel labels = new JLabel();
		labels.setText(id);
		panel.add(labels, BorderLayout.CENTER);
		return panel;

	}

	public AbstractNode createNodeInstance(String s) {
		Class eventclass;
		AbstractNode instance = null;
		try {
			eventclass = Class.forName("client.domainModels.NodeImpl." + s);
			instance = (AbstractNode) eventclass.newInstance();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return instance;
	}

	/**
	 * Called when the user clicks the button or presses Enter in a text field.
	 */
	public void actionPerformed(ActionEvent e) {
		String str;
		if(currentNode.getClass().getSimpleName().equals("TimeStamp")){
			str = ((MyCalen) fields).getDate().getText();
		}else{
			str = ((CombBoxHistory) fields).getItemSelected();
		}
		currentNode.setID(str);
		JScrollPane scrollPanel = new JScrollPane(
				MultiLineTableConstructor.createLogTable(tree.getRootNode()));
		tablesPanel.removeAll();
		tablesPanel.add(scrollPanel, BorderLayout.CENTER);
		tablesPanel.updateUI();

	}

	/**
	 * Called when one of the fields gets the focus so that we can select the
	 * focused field.
	 */
	public void focusGained(FocusEvent e) {
		Component c = e.getComponent();
		if (c instanceof JFormattedTextField) {
			selectItLater(c);
		} else if (c instanceof JTextField) {
			((JTextField) c).selectAll();
		}
	}

	// Workaround for formatted text field focus side effects.
	protected void selectItLater(Component c) {
		if (c instanceof JFormattedTextField) {
			final JFormattedTextField ftf = (JFormattedTextField) c;
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					ftf.selectAll();
				}
			});
		}
	}

	// Needed for FocusListener interface.
	public void focusLost(FocusEvent e) {
	} 

	protected JComponent createEntryFields() {
		
		JPanel panel = new JPanel(new SpringLayout());
		JLabel labels = new JLabel(currentNode.getClass().getSimpleName());
		int fieldNum = 0;

		if(labels.equals("TimeStamp")){
			JPanel textField = new MyCalen();
			fields = textField;
		}else{
			String[] sList = { currentNode.getID() };
			CombBoxHistory textField = new CombBoxHistory(sList,null);
			fields = textField;
		}
		
		labels.setLabelFor(fields);
		panel.add(labels);
		panel.add(fields);

		// Add listeners to each field
		fields.addFocusListener(this);

		SpringUtilities.makeCompactGrid(panel,1, 2, GAP,
				GAP, // init x,y
				GAP, GAP / 2);// xpad, ypad
		return panel;
	}

}
